rm(list = ls())
setwd("/Users/John/Dropbox/")

# --- Load Required Packages ---
library(tidyverse)

# --- Load Data ---
data <- as.data.frame(read_excel("JOP_Replication_Materials/appendix/data/cn_policies_strategic.xlsx", 
                                 sheet = "shortlist_isic_codes")) 
head(data)

# --- Prepare LaTeX table content ---
latex_table <- paste0(
  "\\begin{CJK*}{UTF8}{gbsn}\n",  # Begin the CJK environment
  "\\singlespacing\n",
  "{\\fontsize{8}{10}\\selectfont\n",
  "\\begin{longtable}{|p{2.2cm}|p{12.3cm}|}\n",  # Adjusted column width based on your new table
  "\\caption{ISIC Codes and Industry Descriptions Used for Alternative Strategic Industry Measure} \\label{tab:alt-strategic-isic} \\\n",
  "\\hline\n",
  "\\textbf{ISIC Code} & \\textbf{Industry Description} \\\\\n",
  "\\hline\n",
  "\\endfirsthead\n",
  "\n",
  "\\hline\n",
  "\\textbf{ISIC Code} & \\textbf{Industry Description} \\\\\n",
  "\\hline\n",
  "\\endhead\n",
  "\n",
  "\\hline\n",
  "\\multicolumn{2}{r}{\\textit{Continued on next page}} \\\n",
  "\\endfoot\n",
  "\n",
  "\\hline\n",
  "\\endlastfoot\n"
)


# --- Add data rows ---
for(i in 1:nrow(data)) {
  latex_table <- paste0(latex_table, 
                        data$`ISIC Code`[i], " & ",
                        data$`Industry Description`[i], " \\\\\n")
}

# --- Closing the LaTeX table ---
latex_table <- paste0(latex_table, "\\end{longtable}\n", "}\n", "\\end{CJK*}")

# --- Save to a LaTeX file ---
writeLines(latex_table, "JOP_Replication_Materials/appendix/output/B_table_3.tex")

